October 28, 2018

Buiding Interactive Plots in R -

- Introduction
- Using Plotly
- Plotly Examples
- Using Shiny App
- Shiny Examples
- Shiny with Plotly Example
- Recap
- Questions

Introduction

  • What is Plotly

    • An open source javascript library
    • Allows you to create web based interactive Graphs
    • Intial goal was to suport 3D and Mesh chart that types ggplot2 did not support. -plotly uses the htmlwidget framework, which allows plots to work seamlessly and consistently in various contexts
    • Works with the most basic charts to advance statistical or scientific charts
    • Can be use with local browser or published to a ploty web service
  • What is Shiny

    • Also interactive same as Plotly
    • Shiny can be used as app and can be servered directly from the web
    • Ploty can be in conjuction with Shiny app.

Using Plotly

Basic Example 1

library(plotly)
library(ggplot2)
plot_ly(z = ~volcano)

Basic Example 2

#options(browser = 'FALSE')
t<-plot_ly(midwest, x = ~percollege, color = ~state, 
type = "box",width = 800, height = 300)%>%
layout(margin = list(l = 100))
t
rangeslider(t)

Using Plotly with GGPlotly: GGPLOT

p <- ggplot(txhousing, aes(date, median,
    width = 800, height = 300)) +
    geom_line(aes(group=city), alpha = 0.2)
p

Using Plotly with GGPlotly: GGPLOTLY

#options(browser = 'FALSE')
subplot(
    p, ggplotly(p, tooltip = "city",
                width = 800, height = 300),
    ggplot(txhousing, aes(date, median)) + geom_bin2d(),
    ggplot(txhousing, aes(date, median)) + geom_hex(),
    nrows =2, shareX= TRUE, shareY = TRUE,
    titleY = FALSE, titleX = FALSE
    )

Statistical Example

library(plotly)
library(GGally)
pm<- ggpairs(iris)
ggplotly(pm,width = 800, height = 300)

Plotly online example

- Setup Account in https://plot.ly
- Get username and secret api key from https://plot.ly/settings/api#/
- Use Sys.setenv to setup username and passowrd keys
- Api_create to upload file.

Using Test Figure

- Use api_dowload(id, username) to download plot from site
- Example of dowloading from my account and another public account.

Using Shiny

App R Shiny Example

- Uses app.R file to run
- app.R has three components:
    - UI is theuser interface object
    - server function has to instructions to run build the app
    - shinyApp function takes the UI and Server object and creates shiny app
Shiny applications not supported in static R Markdown documents

Shiny Online Example

- Setup a shiny account https://www.shinyapps.io
- Get username and user key from https://www.shinyapps.io/admin/#/dashboard
- install rconnect library(rsconnect)
- Deploy web app: rsconnect::deployApp('path/to/your/app')
## Preparing to deploy application...DONE
## Uploading bundle for application: 599265...DONE
## Deploying bundle: 1733349 for application: 599265 ...
## Waiting for task: 569590971
##   building: Parsing manifest
##   building: Fetching packages
##   building: Installing packages
##   building: Installing files
##   building: Pushing image: 1790261
##   deploying: Starting instances
##   rollforward: Activating new instances
##   terminating: Stopping old instances
## Application successfully deployed to https://apag101.shinyapps.io/test2/

Shiny App With Plotly

Shiny applications not supported in static R Markdown documents

Recap

QUESTIONS?